package com.aiti.lulian.service.impl;

import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.lulian.entity.SysData;
import com.aiti.lulian.entity.tree.TreeSelect;
import com.aiti.lulian.mapper.SysDataMapper;
import com.aiti.lulian.service.ISysDataService;
import com.aiti.lulian.vo.SysDataVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author ZhouS
 * @CreateDate 2022-07-09
 * @Description:
 */
@Service
public class SysDataServiceImpl extends BaseSerivceImpl<SysDataMapper, SysData> implements ISysDataService {

    @Autowired
    private SysDataMapper sysDataMapper;

    @Override
    public List<SysData> findListPage(Page page, SysData sysData) {

        return sysDataMapper.findListPage(page,sysData);
    }

    @Override
    public SysData byId(String baseId) {
        return sysDataMapper.byId(baseId);
    }

    @Override
    public    List<SysData> getSorts() {
        return sysDataMapper.getSorts();
    }

    @Override
    public List<SysData> likeDictName(String baseName) {
        return sysDataMapper.likeDictName(baseName);
    }

    @Override
    public List<SysDataVo> getScondSort() {
        return sysDataMapper.getScondSort();
    }

    @Override
    public List<String> getBaseNames(List<String> moduleIds) {
        return this.baseMapper.selectBaseNames(moduleIds);
    }

    @Override
    public List<TreeSelect> buildMonitorAreaTreeSelect(List<SysData> list) {
        List<SysData> deptTrees = this.buildDeptTree(list);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    public List<SysData> buildDeptTree(List<SysData> list) {
        List<SysData> returnList = new ArrayList<>();
        List<String> tempList = list.stream().map(SysData::getBaseId).collect(Collectors.toList());
        Iterator<SysData> iterator = list.iterator();
        SysData monitorArea;
        while (iterator.hasNext()) {
            monitorArea = iterator.next();
            if (!tempList.contains(monitorArea.getBaseParentId())) {
                this.recursionFn(list, monitorArea);
                returnList.add(monitorArea);
            }
        }
        if (returnList.isEmpty()) {
            returnList = list;
        }
        return returnList;
    }

    private void recursionFn(List<SysData> list, SysData t) {
        List<SysData> childList = this.getChildList(list, t);
        t.setChildren(childList);
        Iterator<SysData> iterator = childList.iterator();
        while (true) {
            SysData tChild;
            do {
                if (!iterator.hasNext()) {
                    return;
                }
                tChild = iterator.next();
            } while (!this.hasChild(list, tChild));
            Iterator<SysData> it = childList.iterator();
            while (it.hasNext()) {
                SysData n = it.next();
                this.recursionFn(list, n);
            }
        }
    }

    private List<SysData> getChildList(List<SysData> list, SysData t) {
        List<SysData> childList = new ArrayList<>();
        Iterator<SysData> iterator = list.iterator();
        while (iterator.hasNext()) {
            SysData n = iterator.next();
            if (n.getBaseParentId() != null && n.getBaseParentId().equals(t.getBaseId())) {
                childList.add(n);
            }
        }
        return childList;
    }

    private boolean hasChild(List<SysData> list, SysData t) {
        return !this.getChildList(list, t).isEmpty();
    }
}
   